package com.icetec.iceintel.ui.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;


import com.icetec.R;
import com.icetec.iceintel.database.SchemeControlSqlHelper;
import com.icetec.iceintel.model.Device;
import com.icetec.iceintel.model.Scheme;
import com.icetec.iceintel.netrequest.UniResAllocator;

import java.util.ArrayList;

/**
 * Created by 坤道 on 2015/5/31.
 */
public class SchemeDeviceListAdapter extends BaseAdapter{

    private int id;

    private Context context;
    private Scheme scheme;
    private ArrayList<Device> deviceArrayList;
    private SchemeControlSqlHelper schemeControlSqlHelper;
    private LayoutInflater layoutInflater;
    private boolean isEdited = false;
    public SchemeDeviceListAdapter(Context context, int id){
        this.context = context;
        this.id = id;
        layoutInflater = LayoutInflater.from(context);
        schemeControlSqlHelper = new SchemeControlSqlHelper();
        scheme = schemeControlSqlHelper.setGetSingleScheme(id);
        deviceArrayList = scheme.getDeviceArrayList();
    }

    public void setIsEdited(boolean isEdited){
        this.isEdited = isEdited;
    }

    public void updateFromDb(){
        scheme = schemeControlSqlHelper.setGetSingleScheme(id);
        deviceArrayList = scheme.getDeviceArrayList();
    }

    public void saveChangedOndb(){
        schemeControlSqlHelper.updateSingleScheme(scheme);

    }

    public Scheme getScheme(){
        return scheme;
    }

    public void deleteSchemeOndb(){
        schemeControlSqlHelper.deleteScheme(scheme);
    }

    @Override
    public int getCount() {
        return deviceArrayList.size();
    }

    @Override
    public Device getItem(int position) {
        return deviceArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return deviceArrayList.get(position).getId();
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final DeviceHolder deviceHolder;
        final Device device = getItem(position);
        Log.i("device:", String.valueOf(device.getId()) + " position: " + String.valueOf(position) + "bright: " + device.getBrightness() + "color: " + device.getColortemp());
        if(convertView == null){
            convertView = layoutInflater.inflate(R.layout.component_scheme_device_items, null);
            deviceHolder = new DeviceHolder();
            deviceHolder.iconIv = (ImageView) convertView.findViewById(R.id.scheme_devicelist_icon_iv);
            deviceHolder.nameTv = (TextView) convertView.findViewById(R.id.scheme_device_name_tv);
            deviceHolder.brightTv = (TextView) convertView.findViewById(R.id.scheme_device_bright_tv);
            deviceHolder.colorTv = (TextView) convertView.findViewById(R.id.scheme_device_color_tv);
            deviceHolder.bodyRely = (RelativeLayout) convertView.findViewById(R.id.scheme_device_body_rely);
            deviceHolder.seekBarBrightness = (SeekBar) convertView.findViewById(R.id.scheme_device_bright_seekbar);
            deviceHolder.seekBarColortemp = (SeekBar) convertView.findViewById(R.id.scheme_device_color_seekbar);



            convertView.setTag(deviceHolder);

        } else {
            deviceHolder = (DeviceHolder) convertView.getTag();
        }

        if(!isEdited){
            deviceHolder.bodyRely.setVisibility(View.GONE);
        } else {
            deviceHolder.bodyRely.setVisibility(View.VISIBLE);
        }

        if(device.getOnoff()!=null&&device.getOnoff().equals("on")){
            deviceHolder.iconIv.setImageDrawable(UniResAllocator.getResIDbyType(device.getType(), true));
        } else {
            deviceHolder.iconIv.setImageDrawable(UniResAllocator.getResIDbyType(device.getType(), false));
        }

        if(device.getName()!=null) {
            deviceHolder.nameTv.setText(device.getName());
        } else {
            deviceHolder.nameTv.setText(R.string.unnamed_light);
        }

        deviceHolder.brightTv.setText(context.getResources().getText(R.string.brightness_is) + getItem(position).getBrightness());

        deviceHolder.colorTv.setText(context.getResources().getText(R.string.color_temp_is) + getItem(position).getColortemp());

        deviceHolder.seekBarBrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.i("device", String.valueOf(getItem(position).getId()) + " position: " + String.valueOf(position));
                getItem(position).setBrightness(String.valueOf(progress));
                deviceHolder.brightTv.setText(context.getResources().getText(R.string.brightness_is) + String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        deviceHolder.seekBarColortemp.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.i("device", String.valueOf(getItem(position).getId()) + " position: " + String.valueOf(position));
                getItem(position).setColortemp(String.valueOf(progress));
                deviceHolder.colorTv.setText(context.getResources().getText(R.string.color_temp_is) + String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });


        deviceHolder.seekBarBrightness.setProgress(Integer.parseInt(device.getBrightness()));
        deviceHolder.seekBarColortemp.setProgress(Integer.parseInt(device.getColortemp()));

        return convertView;
    }


    public class DeviceHolder{
        ImageView iconIv;
        TextView nameTv;
        TextView brightTv;
        TextView colorTv;
        RelativeLayout bodyRely;
        SeekBar seekBarBrightness;
        SeekBar seekBarColortemp;
    }

}
